<!DOCTYPE HTML>
<html>
<link href="main.css" rel="stylesheet" type="text/css">

<title>SoftPlus</title><body>
<table border="0" align="center">
  <tr>
    <td width="150px"></td>
    <td width="1000px"><img src="banner-01n.gif" width="1000" height="90" alt="Softplus"></td>
  </tr>
  <tr>
    <td valign="top"><?php
      require("navandset.php");
    ?></td>
<td valign="top">

<h1>Catalogue</h1>

<?php
//Connect
$condet = GetGlobalConnectionOptions();
$con = mysql_connect($condet["server"],$condet["username"],$condet["password"]);
if (!$con)
{
	die("Could not connect: " . mysql_error());
}
//Select DB
mysql_select_db($condet["database"], $con);

//Code
//Generate query that will be displayed in the table
$qry = "SELECT item, name, type, platform, stock, price, image FROM tblitems ";
$page = 1;
if($_GET)
{
	switch($_GET["operation"])
	{
		case "Search":
			$page = 1;
			break;
		case "Apply Filters":
			$page = 1;
			break;
		case "Go To Page":
			$page = $_GET["page"];
			break;
		case "Previous":
			if($page > 1)
			{
				$page -= 1;
			};
			break;
		case "Next":
			$page += 1;
			break;
	};
	//Apply search filter
	$qry .= "WHERE name LIKE '%".$_GET["search"]."%' ";
	//Apply type filter
	if($_GET["type"] != "All")
	{
		$qry .= "AND type='".$_GET["type"]."' ";
	};
	//Apply operating system filter
	if($_GET["platform"] != "All")
	{
		$qry .= "AND platform='".$_GET["platform"]."' ";
	};
	//Apply price filter
	if($_GET["pricefilter"])
	{
		$qry .= "AND price>=".$_GET["pricefrom"]." ";
		$qry .= "AND price<=".$_GET["priceto"]." ";
	};
	//Apply stock filter
	if($_GET["stockfilter"])
	{
		$qry .= "AND stock>='".$_GET["minstock"]."' ";
	};
	//Apply supplier filter
	if($_GET["supplier"] != "All")
	{
		$qry .= "AND supplier='".$_GET["supplier"]."' ";
	};
	//Apply sortings
	if($_GET["sort"])
	{
		$qry .= "ORDER BY ";
		switch($_GET["sort"])
		{
			case "nameasc":
				$qry .= "name asc ";
				break;
			case "namedesc":
				$qry .= "name desc ";
				break;
			case "stockasc":
				$qry .= "stock asc ";
				break;
			case "stockdesc":
				$qry .= "stock desc ";
				break;
			case "priceasc":
				$qry .= "price asc ";
				break;
			case "pricedesc":
				$qry .= "price desc ";
				break;
		};
	};
};
$qry .= ";";
$items = mysql_query($qry);
$numitems = mysql_num_rows($items);
$numpages = 1;
$firstitem = 0;
$enditem = 0;
if($numitems > 0)
{
	$numpages = ceil($numitems/10);
	if($page > $numpages)
	{
		$page = $numpages;
	};
	$firstitem = ($page-1)*10;
	$enditem = $page*10-1;
	if($enditem > $numitems)
	{
		$enditem = $numitems-1;
	}
};
?>

<form action="rptcatalogue.php" method="get">
<h2>Search Name:<input type="text" name="search" size="100" maxlength="100" <?php echo "value='".$_GET["search"]."' "; ?>/><input type="submit" name="operation" value="Search"></h2>
<table border="1"><tr>
	<td width="150px" valign="top">
		<table width="150px" border="0">
			<tr>
				<td><h2>Filters</h2><hr/></td>
			</tr>
			<tr>
				<td><h2>Type:</h2></td>
			</tr>
			<tr>
				<td><select name="type">
					<option value='All' selected='selected'>All</option>
					<?php
					$result = mysql_query("SELECT DISTINCT type FROM tblitems ORDER BY type;");
					while($row = mysql_fetch_array($result))
					{
						if($row["type"] == $_GET["type"])
						{
							echo "<option value='".$row["type"]."' selected='selected'>".$row["type"]."</option>";
						}
						else
						{
							echo "<option value='".$row["type"]."'>".$row["type"]."</option>";
						};
					};
					?>
				</select><hr/></td>
			</tr>
			<tr>
				<td><h2>Operating System:</h2></td>
			</tr>
			<tr>
				<td><select name="platform">
					<option value='All' selected='selected'>All</option>
					<?php
					$result = mysql_query("SELECT DISTINCT platform FROM tblitems ORDER BY platform;");
					while($row = mysql_fetch_array($result))
					{
						if($row["platform"] == $_GET["platform"])
						{
							echo "<option value='".$row["platform"]."' selected='selected'>".$row["platform"]."</option>";
						}
						else
						{
							echo "<option value='".$row["platform"]."'>".$row["platform"]."</option>";
						};
					};
					?>
				</select><hr/></td>
			</tr>
			<tr>
				<td><h2><input type="checkbox" name="pricefilter" <?php if($_GET["pricefilter"]=="on") echo "checked='checked'"; ?>/>Price From:</h2></td>
			</tr>
			<tr>
				<td><input type="text" name="pricefrom" size="8" maxlength="8" <?php echo "value='".$_GET["pricefrom"]."' "; ?>/></td>
			</tr>
			<tr>
				<td><h2>To:</h2></td>
			</tr>
			<tr>
				<td><input type="text" name="priceto" size="8" maxlength="8" <?php echo "value='".$_GET["priceto"]."' "; ?>/><hr/></td>
			</tr>
			<tr>
				<td><h2><input type="checkbox" name="stockfilter" <?php if($_GET["stockfilter"]=="on") echo "checked='checked'"; ?> />Stock:</h2></td>
			</tr>
			<tr>
				<td><h2>>=<input type="text" name="minstock" size="5" maxlength="5" <?php echo "value='".$_GET["minstock"]."' "; ?>/></h2><hr/></td>
			</tr>
			<tr>
				<td><h2>Supplier:</h2></td>
			</tr>
			<tr>
				<td><select name="supplier">
					<option value='All' selected='selected'>Any</option>
					<?php
					$result = mysql_query("SELECT supplier, name FROM tblsuppliers ORDER BY name;");
					while($row = mysql_fetch_array($result))
					{
						if($row["supplier"] == $_GET["supplier"])
						{
							echo "<option value='".$row["supplier"]."' selected='selected'>".$row["supplier"]."</option>";
						}
						else
						{
							echo "<option value='".$row["supplier"]."'>".$row["supplier"]."</option>";
						};
					};
					?>
				</select><hr/></td>
			</tr>
		</table>
	</td>
	<td valign="top">
		<table width="840px" border="0" cellpadding="0">
		<tr>
		<th><h3>Image</h3></th>
		<th><h3>Name <input type="radio" name="sort" value="nameasc" <?php if($_GET["sort"]=="nameasc") echo "checked='checked'" ?> /> &and; <input type="radio" name="sort" value="namedesc" <?php if($_GET["sort"]=="namedesc") echo "checked='checked'" ?> /> &or; </h3></th>
		<th><h3>Type - Operating System</h3></th>
		<th><h3>Stock <input type="radio" name="sort" value="stockasc" <?php if($_GET["sort"]=="stockasc") echo "checked='checked'" ?> /> &and; <input type="radio" name="sort" value="stockdesc" <?php if($_GET["sort"]=="stockdesc") echo "checked='checked'" ?> /> &or; </h3></th>
		<th><h3>Price <input type="radio" name="sort" value="priceasc" <?php if($_GET["sort"]=="priceasc") echo "checked='checked'" ?> /> &and; <input type="radio" name="sort" value="pricedesc" <?php if($_GET["sort"]=="pricedesc") echo "checked='checked'" ?> /> &or; </h3></th>
		</tr>
		<?php
			mysql_data_seek($items, $firstitem);
			for($i = $firstitem; $i <= $enditem; $i++)
			{
				$item = mysql_fetch_array($items);
				echo "<tr>";
				echo "<td align='middle' valign='middle'><a href='rptproductinfo.php?item=".$item["item"]."'><img src='itemimages/".$item["image"]."' alt='".$item["image"]."' height='64' width='64' /></a></td>";
				echo "<td align='middle'><a href='rptproductinfo.php?item=".$item["item"]."'><h5>".$item["name"]."</h5></a></td>";
				echo "<td align='middle'><h5>".$item["type"]." - ".$item["platform"]."</h5></td>";
				echo "<td align='middle'><h5>".$item["stock"]."</h5></td>";
				echo "<td align='right'><h5>$".$item["price"]."</h5></td>";
				echo "</tr>";
			};
		?>
		</td></tr>
		</table>
	</td></tr>
	<tr><td></td><td>
		<table border="0" width="840px">
		<tr><td align="left">
		<input type="submit" name="operation" value="Apply Filters">
		</td><td></td><td align="right"><h4>
		<input type="submit" name="operation" value="Go To Page">
		<select name="page">
		<?php
			for($i = 1; $i <= $numpages; $i++)
			{
				if($i == $_GET["page"])
				{
					echo "<option value='".$i."' selected='selected'>".$i."</option>";
				}
					else
				{
					echo "<option value='".$i."'>".$i."</option>";
				};
			};
		?>
		</select>
		of <?php echo $numpages; ?> 
		<input type="submit" name="operation" value="Previous">
		<input type="submit" name="operation" value="Next">
		</h4></td></tr>
		</table>
	</td></tr>
</table></form>
<?php
//Disconnect
mysql_close($con);
?>

</td>
  </tr>
</table>
</body>

</html>
